$(function(){
    var type = new Type();
    type.init().init();
});


function Type(){
    this.init = function(){
        return TYPE;
    };
    
    var TYPE = {
        init : function(){
            TYPE.listeners.bindEvents();
        },
        listeners : {
            bindEvents : function(){
                TYPE.listeners.bindChildLink();
                TYPE.listeners.bindTypeDel();
                TYPE.listeners.bindUpdateType();
            },
            bindChildLink : function(){
                $('#table_agents_manager a.a_type_child').click(function(){
                    var value = $(this).attr('p_name');
                    $hidForm = $('#frm_type_hidden');
                    $hidForm.attr('action', $hidForm.attr('action') + '/type');
                    $hidForm.append('<input type = "hidden" name = "parent_name" value = "'+value+'" />');
                    
                    $hidForm.submit();
                });
            },
            bindUpdateType : function(){
                $('#table_agents_manager a.a_type_update').click(function(){
                    var $dialog = $('#div_type_dialog');
                    var $this = $(this);
                    $dialog.empty();
                    //标识当前选择的RootID
                    $dialog.attr('rootTypeId', $this.attr('t_id'));
                    
                    TYPE.funs.initTypeDialog($dialog);
                    
                    $.post(contextPath + '/Type/listChildType', {
                        'root_id' : $this.attr('t_id')
                    }, function(jsonResponse){
                        if('null' == jsonResponse.data){
                            TYPE.funs.initTypeDialogContent($dialog, []);
                            return;
                        }
                        var childTypeId = [];
                        
                        //                        console.debug(jsonResponse);return;
                        $.each($.parseJSON(jsonResponse.data), function(index, child){
                            console.debug(index);
                            childTypeId.push(child.id);
                        });
                        TYPE.funs.initTypeDialogContent($dialog, childTypeId);
                    }, 'json');
                });
                
            },
            bindTypeDel : function(){
                $('#table_agents_manager a.a_type_delete').click(function(){
                    var id = $(this).attr('t_id');
                    $.post(SYSTEM_DATA.current_url + '/typeDel', {
                        'id' : id
                    }, function(jsonResult){
                        if(0 == jsonResult.status){
                            alert(jsonResult.info);
                        }else{
                            alert(jsonResult.info);
                            window.location = SYSTEM_DATA.current_url + '/type';
                        }
                    }, 'json');
                });
            }
        },
        funs :{
            initTypeDialog : function($dialog){
                $dialog.dialog({
                    'title' : '选择类型',
                    'resizable': false,
                    'width' : 450,
                    'height':340,
                    'modal': true,
                    'buttons': {
                        "确定": function() {
                            TYPE.funs.updateTypeData($dialog);
                            $( this ).dialog( "close" );
                        },
                        '取消': function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            },
            //添加DIalog内容，并选中子类型
            initTypeDialogContent : function($dialog, childTypeId){
                var typeValue = [];
                $.each($.parseJSON(TYPE_DATA.child_type_list), function(index, child){
                    if(-1 != $.inArray(child.id, childTypeId)){
                        typeValue.push(child.name + "<input type = 'checkbox' checked = 'checked' value = '"+ child.id +"'/>");
                    }else{
                        typeValue.push(child.name + "<input type = 'checkbox' value = '"+ child.id +"'/>");
                    }
                //                    console.debug(child);
                });
                
                $dialog.append(typeValue.join(''));
            },
            
            updateTypeData : function($dialog){
                $checked = $dialog.find(':checked');
                var checkedId = [];
                
                $.each($checked, function(index, chk){
                    checkedId.push($(chk).val());
                })
                
                $.post(contextPath + '/Type/updateChildType', {
                    'root_id' : $dialog.attr('rootTypeId'),
                    'child_id' : checkedId.join(',')
                }, function(jsonResult){
                    if(1 == jsonResult.status){
                        alert('更新成功');
                    }else{
                        alert('更新失败');
                    }
                }, 'json');
            }
        }
    };
}
